package com.hd.tiyu.yuejian.Adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hd.tiyu.yuejian.JavaBean.Tiwen;
import com.hd.tiyu.yuejian.JavaBean.User;
import com.hd.tiyu.yuejian.Model.ListsModel;
import com.hd.tiyu.yuejian.R;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by admin on 2017/11/8.
 */

public class WenTiAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<Tiwen> mDataList;
    private LayoutInflater mLayoutInflater;

    public WenTiAdapter(Context mContext, List<Tiwen> mDataList){
        this.mContext = mContext;
        this.mDataList=mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView username,wenti,time;

        public ViewHolder(View itemView) {
            super(itemView);
            username=itemView.findViewById(R.id.wenti_username);
            wenti=itemView.findViewById(R.id.wenti_count);
            time=itemView.findViewById(R.id.wenti_time);
        }
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=mLayoutInflater.from(parent.getContext()).inflate(R.layout.tiwenlist,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Tiwen data=mDataList.get(position);
        if (data!=null){
            final ViewHolder viewHolder=(ViewHolder) holder;
            String userid=data.getUser_id();
            ListsModel model=new ListsModel();
            Call<User> call=model.Userinfo(userid);
            call.enqueue(new Callback<User>() {
                @Override
                public void onResponse(Call<User> call, Response<User> response) {
                    User userdata=response.body();
                    if(userdata!=null) {
                        viewHolder.username.setText(userdata.getUser_name());
                    }
                }

                @Override
                public void onFailure(Call<User> call, Throwable t) {

                }
            });
            viewHolder.wenti.setText(data.getQuestion());
            viewHolder.time.setText(data.getQuestion_time());
        }
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }
}
